package com.example.lt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lt.constant.MessageConstant;
import com.example.lt.exception.CommentException;
import com.example.lt.exception.SupportFailException;
import com.example.lt.mapper.CommentMapper;
import com.example.lt.pojo.dto.AddCommentDTO;
import com.example.lt.pojo.dto.CommentDTO;
import com.example.lt.pojo.entity.Comment;
import com.example.lt.pojo.entity.Consumer;
import com.example.lt.pojo.vo.CommentVO;
import com.example.lt.result.Result;
import com.example.lt.service.CommentService;
import com.example.lt.service.ConsumerService;
import com.example.lt.service.UserSupportService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 小涛Tao
 * @Date: 2024/06/03/17:26
 * version 1.0 注释：
 **/
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    private final CommentMapper commentMapper;

    private final UserSupportService userSupportService;

    private final ConsumerService consumerService;

    /**
     * 添加用户评论
     * @param addCommentDTO
     * @return
     */
    public Result addComment(AddCommentDTO addCommentDTO) {
        Comment comment = BeanUtil.copyProperties(addCommentDTO, Comment.class);
        comment.setType(addCommentDTO.getNowType());
        int flag = commentMapper.insert(comment);
        if (flag > 0) {
            return Result.success(MessageConstant.COMMENT_SUCCESS);
        } else {
            throw new CommentException(MessageConstant.COMMENT_FAIL);
        }
    }

    /**
     * 删除用户评论
     * @param id 评论的主键id
     * @return
     */
    public Result deleteComment(Integer id, Integer userId) {
        // 后续这里的TODO要取消注释
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq(userId != null, "user_id", userId);
        int flag = commentMapper.delete(queryWrapper);
        if (flag > 0) {
            // 删除评论成功后要去删除点赞表中的相应评论点赞记录
            userSupportService.deleteByComId(id);
            return Result.success(MessageConstant.DELETE_SUCCESS);
        } else {
            throw new CommentException(MessageConstant.DELETE_FAIL);
        }
    }

    /**
     * 获取歌单或歌曲的所有评论
     * @param songListId
     * @return
     */
    public List<CommentVO> getSongOrListComment(Integer songListId, Integer songId) {
        // songListId为歌单id，songId为歌曲id，哪一个不为空就是查询哪一个的所有评论
        List<Comment> comments = lambdaQuery()
                .eq(songListId != null, Comment::getSongListId, songListId)
                .eq(songId != null, Comment::getSongId, songId)
                .list();

        if (comments.size() == 0) { // 如果没有评论就直接返回null了
            return new ArrayList<>();
        }

        // 通过stream流把List<Comment>集合中的userId抽出来成为一个集合List<Integer>
        List<Integer> userId = comments.stream().map(Comment::getUserId).collect(Collectors.toList());
        List<Consumer> consumerList = consumerService.listByIds(userId);

        List<CommentVO> commentVOS = new ArrayList<>();
        for (Comment comment : comments) {
            CommentVO commentVO = BeanUtil.copyProperties(comment,CommentVO.class);

            for (Consumer consumer : consumerList) { // 把用户名和用户头像赋值给返回结果CommentVO
                if (commentVO.getUserId() == consumer.getId()) {
                    commentVO.setUsername(consumer.getUsername());
                    commentVO.setAvator(consumer.getAvator());
                }
            }

            commentVOS.add(commentVO);
        }
        return commentVOS;
    }

    /**
     * 更新当前评论的总点赞数
     * @param commentDTO
     * @return
     */
    public Result updateSupportCum(CommentDTO commentDTO) {
        Comment comment = BeanUtil.copyProperties(commentDTO, Comment.class);
        if (commentMapper.updateById(comment) > 0) {
            return Result.success(MessageConstant.ALTER_SUCCESS);
        } else {
            throw new SupportFailException(MessageConstant.ALTER_FAIL);
        }
    }

}
